﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;

public partial class AddSeries : System.Web.UI.Page
{
    ManageDatabase data;

    Serie series;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            //User.Identity.Name;
            //CustomIdentity ident = (CustomIdentity)User.Identity;
            //ident.Name, ident.UserType;
            //if (HttpContext.Current.User.IsInRole("page_admin"))

            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (!IsPostBack)
            {
                GetGenres();
                Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
            }
            series = new Serie();
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    private void GetGenres() 
    {
        ListItemCollection allGenres = data.GetGenres();
        AddSeriesGenres.Items.Clear();
        foreach (ListItem item in allGenres)
        {
            AddSeriesGenres.Items.Add(item);
        } 
    }
    protected void SeriesCreate_Click(object sender, EventArgs e)
    {
        try{
            series.titleHun = AddSeriesTitleHun.Text;
            series.titleOrig = AddSeriesTitleOrig.Text;
            if (series.titleHun == "" || series.titleHun == " ")
            {
                Alert.Show("Kérem adja meg a sorozat címét!");
                return;
            }
            series.genres = Functions.GetSelectedListBoxItems(AddSeriesGenres);
            series.directors = AddSeriesDirectorName.Text;
            series.writers = AddSeriesWriterName.Text;
            series.stars = AddSeriesStarName.Text;
            series.releaseDate = new DateTime(1800, 01, 01);
            if (AddSeriesReleaseDate.Text != "")
            {
                try
                {
                    series.releaseDate = DateTime.Parse(AddSeriesReleaseDate.Text);
                }
                catch (Exception)
                {
                    Alert.Show("Érvénytelen a megjelenés dátuma!");
                    return;
                }
            }

            if (AddSeriesEmbedVideo.Text.IndexOf("&") == -1)
            {
                series.videoUrl = AddSeriesEmbedVideo.Text.Substring((AddSeriesEmbedVideo.Text.IndexOf("v=") + 2), (AddSeriesEmbedVideo.Text.Length - AddSeriesEmbedVideo.Text.IndexOf("v=") - 2));
            }
            else
            {
                series.videoUrl = AddSeriesEmbedVideo.Text.Substring((AddSeriesEmbedVideo.Text.IndexOf("v=") + 2), (AddSeriesEmbedVideo.Text.IndexOf("&") - AddSeriesEmbedVideo.Text.IndexOf("v=") - 2));
            }

            series.description = AddSeriesDescription.Text;

            series.imgUrl = "";
            if (AddSeriesImg.HasFile)
            {
                try
                {
                    if (AddSeriesImg.PostedFile.ContentType == "image/pjpeg" || AddSeriesImg.PostedFile.ContentType == "image/x-jpeg") //image/gif,image/x-png
                    {
                        if (AddSeriesImg.PostedFile.ContentLength < 512000)
                        {
                            string filename = "series_" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".jpeg";
                            AddSeriesImg.SaveAs(Server.MapPath("~/Pic/") + filename);
                            series.imgUrl = filename;
                        }
                        else
                        {
                            StatusLabel.Text = "A maximális képméret 500 kb!";
                            return;
                        }
                    }
                    else
                    {
                        StatusLabel.Text = "Az alábbi képformátum nem támogatott: " + AddSeriesImg.PostedFile.ContentType;
                        return;
                    }
                }
                catch (Exception)
                {
                    StatusLabel.Text = "Hiba történt a kép feltöltése során.";
                    return;
                }
            }

            Boolean success = data.InsertSeries(series, Functions.GetUserId(), User.IsInRole("admin"));

            if (!success)
            {
                Alert.Show("Hiba történt a sorozat létrehozása során!");
            }
            else
            {
                Response.Redirect("Series.aspx");
            }

        }catch(Exception){
            Alert.Show("Hiba történt az sorozat létrehozása során!");
        }
        
    }
    protected void SeriesCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Series.aspx");
    }
}